# Author fengzhilaoling
# coding=utf-8
# @Time    : 2020/10/2 上午11:53
# @Site    : 
# @File    : settings.py
# @Software: PyCharm
# @contact: fengzhilaoling@163.com
from os import path

BASE_DIR = path.dirname(path.dirname(path.abspath(__file__)))  # 项目根目录


# 数据库连接拼接
def get_db_uri(db_info):
    engine = db_info.get("ENGINE") or "mysql"
    driver = db_info.get("DRIVER") or "pymysql"
    user = db_info.get("USER") or "root"
    password = db_info.get("PASSWORD") or ""
    host = db_info.get("HOST") or "localhost"
    port = db_info.get("PORT") or "3306"
    name = db_info.get("NAME") or ""

    # 使用的数据库+数据库驱动://用户名:密码@主机:端口/那个库
    return f"{engine}+{driver}://{user}:{password}@{host}:{port}/{name}"


class Config:
    BEBUG = False  # 开发使用
    TESTING = False  # 测试使用
    SQLALCHEMY_TRACK_MODIFICATIONS = False


# 开发环境
class DevelopConfig(Config):
    BEBUG = True
    SQLALCHEMY_TRACK_MOOIFICATIONS = True
    db_info = {
        "ENGINE": "mysql",
        "DRIVER": "pymysql",
        "USER": "fengling",
        "PASSWORD": "fengling",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "flask_db",
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


# 测试环境
class TestConfig(Config):
    TestConfig = True
    db_info = {
        "ENGINE": "mysql",
        "DRIVER": "pymysql",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "FlaskDB",
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


# 演示环境
class StagineConfig(Config):
    db_info = {
        "ENGINE": "mysql",
        "DRIVER": "pymysql",
        "USER": "root",
        "PASSWORD": "123456",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "FlaskDB",
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


# 生产环境
class ProductConfig(Config):
    db_info = {
        "ENGINE": "mysql",
        "DRIVER": "pymysql",
        "USER": "fengling",
        "PASSWORD": "fengling",
        "HOST": "localhost",
        "PORT": "3306",
        "NAME": "flask_db",
    }

    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


envs = {
    "develop": DevelopConfig,
    "testing": TestConfig,
    "staging": StagineConfig,
    "product": ProductConfig,
    "default": DevelopConfig,
}
